﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Pipeline.Design;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Design;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

namespace Pestre.SqlServer.SSIS.DataFlowComponent
{
    /*
     * VirtualInputColumns contains an array of VirtualInputColumn instances, and provides
     * method to load and save these data.
     * 
     * It's a contener used to keep information relative to a all Virtual Column available for
     * our component and their usage.
     */
    class VirtualInputColumns90
    {
        // Array of VirtualInputColumn instances
        private ArrayList _vicColumns = null;
        // Component MetaData
        private IDTSComponentMetaData90 _md = null;

        /*
         * Instanciate VirtualInputColumns from a Custom Property.
         */
        public VirtualInputColumns90(IDTSComponentMetaData90 md)
        {
            String sCustomProperty;

            _md = md;
            try
            {
                _vicColumns = new ArrayList();
                sCustomProperty = (String)md.CustomPropertyCollection[ErrorOutputDescription.PROP_VIRTUAL_COLUMNS].Value;
                foreach (String sProperty in sCustomProperty.Split(';'))
                    if (sProperty.Length > 0)
                    {
                        VirtualInputColumn90 vicColumn = new VirtualInputColumn90(sProperty);
                        _vicColumns.Add(vicColumn);
                    }
            }
            catch (Exception ex)
            {
                bool oCancel;
                md.FireError(0, md.Name, "Loading '" + ErrorOutputDescription.PROP_VIRTUAL_COLUMNS + "' parameters : " + ex.Message, "", 0, out oCancel);
            }
        }

        /*
         * Store all data in a custom property.
         */
        public void Save()
        {
            String sCustomProperty = "";

            foreach (VirtualInputColumn90 vicColumn in _vicColumns)
                sCustomProperty += vicColumn.ToString();
            _md.CustomPropertyCollection[ErrorOutputDescription.PROP_VIRTUAL_COLUMNS].Value = sCustomProperty;
        }

        /*
         * Get the Array of VirtualInputColumn instances.
         */
        public ArrayList Collection
        {
            get { return _vicColumns; }
        }

        /*
         * Set usage type of all VirtualInputColumn instances to "UnUsed".
         */
        public void SetAllUnused()
        {
            foreach (VirtualInputColumn90 vicColumn in _vicColumns)
                vicColumn.OutputColumn_IsUsed = false;
        }

        /*
         * This method gives an unique name to the output column.
         * A number is incremented and added at the end of the column name.
         */
        public void GiveUniqueAlias(VirtualInputColumn90 virCol)
        {
            String sUniqueName = virCol.InputColumn_Name;
            int iNumber = 1, iNbIter = 0;
            bool bIsUnique = false;

            while (bIsUnique == false && iNbIter < Int32.MaxValue)
            {
                bIsUnique = true;
                foreach (VirtualInputColumn90 virColIter in _vicColumns)
                {
                    if (virColIter.Input_ID == virCol.Input_ID && virColIter.InputColumn_LineageID == virCol.InputColumn_LineageID)
                        continue ; // Don't compare the column with itself

                    if (sUniqueName.Equals(virColIter.OutputColumn_Name)) // There is a conflict, the name is not unique.
                    {
                            bIsUnique = false;
                            sUniqueName = virCol.InputColumn_Name + "(" + iNumber.ToString() + ")"; // Try a new name
                            iNumber++;
                            break;
                    }
                }
                iNbIter++;
            }
            // Save unique name
            virCol.OutputColumn_Name = sUniqueName;
        }

    }
}